# -*- coding: utf-8 -*-
"""
 ----------------------------------------
|File Name:     prng4
|Author:        WYT
|date:          2021/7/2
 ----------------------------------------
|  Change Activity:
|                   2021/7/2:
-----------------------------------------
"""


class PrngNewstate():

    def __init__(self):
        self.i = 0
        self.j = 0
        self.S = []

    """
    @:param key 数组对象
    """
    def init(self, key):
        i: int
        j: int
        t: int

        while i < 256:
            self.S[i] = i
            i += 1

        j = 0
        i = 0
        while i < 256:
            j = (j + self.S[i] + key[i % key.length]) & 255
            t = self.S[i]
            self.S[i] = self.S[j]
            self.S[j] = t
            i += 1

    def next(self):
        t: int
        self.i = (self.i + 1) & 255
        self.j = (self.j + self.S[self.i]) & 255
        t = self.S[self.i]
        self.S[self.i] = self.S[self.j]
        self.S[self.j] = t
        return self.S[(t + self.S[self.i]) & 255]
